package chatNI;



import java.io.IOException;
import java.net.*;

/* 
 * @author Yahya-Albaba
*/

public class ThreadReceiveMsg extends Thread {


	/**
	 * @param  cni is the instance of the chat network interface that we are using   
	 * @param  rcvMsgSock represents the socket that we use to receive a message
	 * @param  port is the port for this application
	*/
	
	
	private static ChatNI cni;
	private static DatagramSocket rcvMsgSock;
	private int port;
	
	public ThreadReceiveMsg(ChatNI cni, int port){
		this.cni=cni;
		this.port=port;
	}
	
	public void run(){
		DatagramPacket packet;
		byte[] msgRcvd;
		try{
			rcvMsgSock=new DatagramSocket(port);
			int bufferSize=rcvMsgSock.getReceiveBufferSize();
			while(!interrupted()){
				msgRcvd=new byte[bufferSize];
				packet=new DatagramPacket(msgRcvd, msgRcvd.length);
				rcvMsgSock.receive(packet);
				cni.Receive(packet);
			}rcvMsgSock.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

	